//
//  KKMyMessageController.m
//  DoMi
//
//  Created by 康凯 on 2016/11/16.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKMyMessageController.h"
#import "KKTopSelectView.h"
#import "KKMyHttp.h"
#import "KKMessageM.h"
#import "KKMyMessageDetailVC.h"
#import "KKCollectDocumentCell.h"
#import "KKDraftCell.h"
@interface KKMyMessageController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//0代表未读1代表已读
@property (nonatomic,assign) int flag;

@property(nonatomic,strong)NSArray *msgDatas;
@end

@implementation KKMyMessageController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getMessageList];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"我的消息";
    [self initView];
}

#pragma mark 初始化view
-(void)initView{
    self.flag=0;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    
    KKTopSelectView *topItem=[[[NSBundle mainBundle] loadNibNamed:@"KKTopSelectView" owner:nil options:nil] lastObject];
    [topItem initData:@[@"未读",@"已读"]];
    topItem.selectBlock=^(int flag){
        self.flag=flag;
        [self getMessageList];
    };
    [self.topView addSubview:topItem];
    
    //取消group样式头部空白
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 0.1f)];
    [self.tableView registerNib:[UINib nibWithNibName:@"KKCollectDocumentCell" bundle:nil] forCellReuseIdentifier:@"KKCollectDocumentCell"];
}

#pragma mark tableview代理
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    KKCollectDocumentCell *msg=(KKCollectDocumentCell *)cell;
    [msg initMsgData:self.msgDatas[indexPath.row]];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.msgDatas.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.flag==0){//未读
        return [tableView dequeueReusableCellWithIdentifier:@"KKCollectDocumentCell"];
    }else if(self.flag==1){//已读
        return [tableView dequeueReusableCellWithIdentifier:@"KKCollectDocumentCell"];
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 8;
    }
    return 0.1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KKMessageM *msg=self.msgDatas[indexPath.row];
    KKMyMessageDetailVC *detail=[[KKMyMessageDetailVC alloc] init];
    detail.title1=msg.title;
    detail.content=msg.context;
    detail.time=msg.msgtime;
    detail.msgId=msg.id;
    [self.navigationController pushViewController:detail animated:YES];
}

#pragma mark 接口--消息列表
-(void)getMessageList{
    [self showDialog];
    [KKMyHttp getMyMsg:[NSString stringWithFormat:@"%d",self.flag] success:^(id data) {
        self.msgDatas=data;
        [self.tableView reloadData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}
@end
